package com.tools.web.interceptor.access;

import com.tools.common.object.Note;
import com.tools.web.work.ResultCode;

import java.util.Map;
import java.util.Objects;

/**
 * 权限验证拦截器所需参数配置项的包装类
 * */
@Note("权限验证拦截器所需参数配置项的包装类")
public class AccessSource {

    @Note("访问权限不足时访问给客户端的响应信息")
    private String accessFailErrorMessage;

    @Note("若开启了时间范围验证，且接口的开放时间还没到，就响应该信息")
    private String unreleasedErrorMessage;

    public AccessSource() {
        this.accessFailErrorMessage = ResultCode.ACCESS_FAIL.toJSON();
        this.unreleasedErrorMessage = ResultCode.NOT_YET_OPEN.toJSON();
    }

    public static AccessSource of() {
        return new AccessSource();
    }

    @Note("读取 properties 里的配置项，转为实例返回")
    public static AccessSource from(Map properties) {
        AccessSource source = new AccessSource();
        if(properties == null || properties.isEmpty()) return source;
        source.setAccessFailErrorMessage((String) properties.get("access-manager-accessFailErrorMessage"));
        source.setUnreleasedErrorMessage((String) properties.get("access-manager-unreleasedErrorMessage"));
        return source;
    }

    public String getAccessFailErrorMessage() {
        return accessFailErrorMessage;
    }

    public void setAccessFailErrorMessage(String accessFailErrorMessage) {
        if(accessFailErrorMessage != null && !accessFailErrorMessage.isEmpty()) {
            this.accessFailErrorMessage = accessFailErrorMessage;
        }
    }

    public String getUnreleasedErrorMessage() {
        return unreleasedErrorMessage;
    }

    public void setUnreleasedErrorMessage(String unreleasedResponseMessage) {
        if(unreleasedResponseMessage != null && !unreleasedResponseMessage.isEmpty()) {
            this.unreleasedErrorMessage = unreleasedResponseMessage;
        }
    }

    @Override
    public String toString() {
        return "AccessSource{" +
                "accessFailErrorMessage=" + accessFailErrorMessage +
                ", unreleasedErrorMessage=" + unreleasedErrorMessage +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AccessSource source = (AccessSource) o;
        return Objects.equals(accessFailErrorMessage, source.accessFailErrorMessage) && Objects.equals(unreleasedErrorMessage, source.unreleasedErrorMessage);
    }

    @Override
    public int hashCode() {
        return Objects.hash(accessFailErrorMessage, unreleasedErrorMessage);
    }

    public AccessSource accessFailErrorMessage(String accessFailErrorMessage) {
        this.setAccessFailErrorMessage(accessFailErrorMessage);
        return this;
    }

    public AccessSource unreleasedErrorMessage(String unreleasedErrorMessage) {
        this.setUnreleasedErrorMessage(unreleasedErrorMessage);
        return this;
    }
}
